<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<div id="page-content">
  <div class="row">

    <div class="col-lg-12">
      <div class="panel">
        <div class="panel-heading">
          <h3 class="panel-title">部门管理</h3>
        </div>
        <div class="panel-body">
          <div class="row">
            <div class="col-md-3">
              <ul id="tree_department" class="ztree"></ul>
            </div>
            <div class="col-md-9">
              <div class="form-inline">
                <div class="row">
                  <div class="col-sm-12 table-toolbar-left">
                    <%--
                    <btn id="btn_add_duty" class="btn btn-primary btn-sm">
                        <i class="ti-plus"> </i> 添加成员
                    </btn>
                    --%>
                  </div>
                </div>
              </div>
              <div class="jqGrid_wrapper">
                <table id="duty_table"></table>
                <div id="duty_page"></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</div>

<script>

    $(function(){

        init_tree_department();

    });

    function init_tree_department() {

        var setting = {
            async: {
                enable: true,
                url:'<%=path%>/main/department/tree',
                autoParam: ['id'],
                type: 'get',
                dataType: 'json'
            },
            callback: {
                onAsyncSuccess: zTreeOnAsyncSuccess,

                onClick: zTreeOnClick,
                onCheck: zTreeOnCheck
            },
//            check: {
//                enable: true,
//                chkStyle: 'checkbox',
//                chkboxType:  { 'Y' : '', 'N' : '' }
//            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: 'id',
                    pIdKey: 'pId',
                    rootPId: ''
                }
            },

            view: {
                autoCancelSelected: false,
                dblClickExpand: false,
                selectedMulti: false
            }
        };

        $.fn.zTree.init($('#tree_department'), setting);

    }


    function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
        var zTree = $.fn.zTree.getZTreeObj('tree_department');
        if (treeNode) {
            if (treeNode.id === '0') {
                var nodes = treeNode.children;
                zTree.selectNode(nodes[0]);
                init_grid_duty(nodes[0].id);
            }
        } else {
            var rootNode = zTree.getNodes()[0];
            if (rootNode.isParent) {
                zTree.expandNode(rootNode, true, true, true);
            }
        }
    }




    function zTreeOnClick(event, treeId, treeNode){
        var id = treeNode.id;
      //  if (id && id != "0") {
            $('#duty_table').jqGrid().setGridParam({datatype:'json',postData: {departmentId: id}, page: 1}).trigger('reloadGrid');
       // }
    }

    function zTreeOnCheck(event, treeId, treeNode){

    }






    function init_grid_duty(departmentId) {

        __init_jqgrid('duty_table', 'duty_page',
            '<%=path%>/main/duty/list_department',
            ['成员', '岗位名称', '身份', '用户id', 'id'],
            [
                {name: 'userName', index: 'userName', width: 60, sortable: true, stype: 'text', searchoptions: {sopt: ['cn']}},
                {name: 'postName', index: 'postName', width: 60, sortable: true, stype: 'text', searchoptions: {sopt: ['cn']}},
                {name: 'dutyState', index: 'dutyState', width: 60, sortable: true, stype: 'text', searchoptions: {sopt: ['cn']}},
                {name: 'userId', index: 'userId', width: 60,  hidden: true},
                {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
            ],
            true,
            {
                postData: {departmentId: departmentId}
            }
        );

    }



    function fn_dialog_member_select(callback) {
        var rowId = $('#duty_table').jqGrid('getGridParam', 'selrow');
        if (rowId) {
            callback($('#duty_table').jqGrid('getRowData', rowId));
        } else {
            //__toastr_warning('请选择一个成员');
            callback();
        }
    }

</script>